<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <h3>V部落博客管理平台</h3>
                <el-dropdown @command="dropdownCallBack">
          <span class="el-dropdown-link">
            {{loginUser.nickname}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="settings">个人中心</el-dropdown-item>
                        <el-dropdown-item divided command="logout">注销</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu router>

                        <el-submenu :index="i+''" v-for="(item,i) in this.$router.options.routes" v-if="!item.hidden && item.meta.hasChild && checkRoles(item.meta.roles) == 200">
                            <template slot="title">
                                <i :class="item.meta.icon" aria-hidden="true"></i>
                                <span>{{item.meta.title}}</span>
                            </template>
                            <el-menu-item :index="child.path" v-for="(child,index) in item.children" v-if="checkRoles(child.meta.roles) == 200">
                                <span slot="title">{{child.meta.title}}</span>
                            </el-menu-item>
                        </el-submenu>

                        <div v-for="(item,i) in this.$router.options.routes" v-if="!item.hidden && !item.meta.hasChild">
                            <el-menu-item :index="child.path" v-for="(child,index) in item.children" v-if="checkRoles(child.meta.roles) == 200">
                                <i :class="child.meta.icon" aria-hidden="true"></i>
                                <span slot="title">{{child.meta.title}}</span>
                            </el-menu-item>
                        </div>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 10px">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$route.meta.title}}</el-breadcrumb-item>
                    </el-breadcrumb>
                    <div v-if="this.$route.path == '/home'">
                        <h2 style="color: #409EFF;text-align: center">欢迎来到V部落博客管理平台</h2>
                    </div>
                    <router-view/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    export default {
        name: 'Home',
        data() {
            return{
                loginUser : JSON.parse(window.sessionStorage.getItem('loginer'))
            }
        },
        methods : {
            // 检查用户角色是否具备对应菜单需要角色
            checkRoles(needRoles) {
                let result = 500;
                let roles = this.loginUser.roles;// [{id,rname,rnameZh},{}]
                roles.forEach((role,i) => {
                    needRoles.forEach((needRole,index) => {
                        if (role.rname == needRole) {
                            result = 200;
                        }
                    })
                })
                return result;
            },
            // Header下拉菜单回调
            dropdownCallBack(item) {
                if (item == 'logout') {
                    this.$confirm('是否确认注销登录, 是否继续?', '注销', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.$getRequest('/logout').then(resp => {
                            if (resp) {
                                // 将 session 中 loginer 删除
                                window.sessionStorage.removeItem('loginer');
                                // 跳转登录页
                                this.$router.replace('/');
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消操作'
                        });
                    });
                }
            }
        }
    }
</script>

<style scoped>
    .homeHeader{
        background: #409EFF;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .homeHeader h3{
        color: #fff;
    }
    .el-dropdown-link {
        cursor: pointer;
        color: #fff;
    }
    .el-icon-arrow-down {
        font-size: 12px;
    }
    .font-margin{
        margin-right: 10px;
        color: #409EFF;
    }
</style>